# -*- coding: utf-8 -*-
"""
-------------------------------------------------
   File Name：     num_recognize
   Description :
   Author :       'li'
   date：          2021/7/4
-------------------------------------------------
   Change Activity:
                   2021/7/4:
-------------------------------------------------
"""
from ml.modules.container_num_recognition.single_container_recognition.module.check_result_template.result_mapping import \
    wrong_right_mapping


class NumRecognizer:
    def __init__(self, torch_recognizer):
        self.torch_recognizer = torch_recognizer

    @staticmethod
    def _post_processing_recognition_result(res, shape):
        """
        :param res:
        :return:
        """

        if len(res) > 0 and 'U' == res[-1]:
            res = res.replace('0', 'O')
        elif 0 < len(res) < 4 and res[-1].isalpha():
            res = res.replace('0', 'O')
        if res in wrong_right_mapping:
            res = wrong_right_mapping[res]
        if 1.5 < shape[0] / shape[1] < 3 and len(res) > 2:
            res = ''
        if 'I' == res:
            return '1'
        return res

    def recognition_container_textarea(self, original_textarea):
        """
        recognition container textarea
        :param original_textarea:
        :return:
        """
        res = self.torch_recognizer.recognize_interface(original_textarea)
        shape = original_textarea.shape
        processed_result = self._post_processing_recognition_result(res, shape)
        return processed_result
